#!/usr/bin/env

# INSTRUCTIONS:
# 
# (1) Change ONE line below so that SmartPhone has all the functionality
#     of the cell phone.  
#
# (2) Extend the SmartPhone class with a chat feature.
#     Execute included unit tests to check the SmartPhone code.
#     Run the unit test from a shell with
#
#         python -m testsmartphone.py
#
# (2) Extend the SmartPhone class with a chat feature.

from cellphone import CellPhone
from textmessage import TextMessage

class SmartPhone(CellPhone):
    '''SmartPhone is a CellPhone with a chat feature.  It can print
    chat history with all other phones.
    '''

    def __init__(self, owner):
        '''Initialize the SmartPhone object.
        owner    -- optional string name of the phone owner
        '''
        super(SmartPhone, self).__init__(owner)
        self.chathistory = {}
    
    def sendChat(self, recipient, msgtext):
        '''Send text message to another phone.

        recipient -- instance of another SmartPhone
        msgtext   -- string with a message text that is sent

        No Return value.
        Raise ValueError exception if recipient cannot receive chats.
        '''
        message = TextMessage(sender, recipient, msgtext)
        recipient.receivechat(message)
        print self, "sent a text to", recipient

        if self.chathistory.get(recipient) == None:
            self.chathistory[recipient] = [str(message)]
        else:
            self.chathistory[recipient].append(str(message))
        
        return

    def receiveChat(self, message):
        '''Accept text message from another phone.

        message  -- an instance of the TextMessage class being received

        No Return value.
        ''' 
        print self, "received a text from", message.sender
        
        if self.chathistory.get(message.sender) == None:
            self.chathistory[message.sender] = [str(message)]
        else:
            self.chathistory[message.sender].append(str(message))
                                                                 
        return

    def getChatHistory(self, other_phone):
        '''Return chat history with another SmartPhone.

        other_phone  -- instance of another SmartPhone for which the
                        chat history is being looked up

        Return list of TextMessage objects.
        '''
        return self.chathistory[other_phone.owner]

    def printChatHistory(self, other_phone):
        '''Print chat history with another SmartPhone.

        other_phone  -- instance of another SmartPhone

        No return value.
        '''
        chathistory = getChatHistory(other_phone)
        if chathistory != None:
            print "Chat History with", other_phone
            for chat in chathistory[other_phone.owner]:
                print chat
            else:
                print "No Chat History with", other_phone

        return
